import { createStore } from "vuex";
import vuexPer from "vuex-persistedstate";
export default createStore({
  state: {
    list: [],
  },
  mutations: {
    setList(state, val) {
      if (localStorage.getItem("vuex")) {
        state.list = JSON.parse(localStorage.getItem("vuex")).list;
      } else {
        state.list = val;
      }
      //手动从本地存储中拿东西 要不然一进首页就会从新请求数据 content就为空了
    },
    //把数据存在vuex中
    addMsg(state, obj) {
      let { index, con } = obj;
      console.log(state.list);
      state.list[index].content.push({
        title: con,
        time: new Date().toLocaleString(),
        // status: true,
      });
    },
    remove(state, obj) {
      let { index, ind } = obj;
      // index是第一层下标 ind是第二层下标
      state.list[index].content.splice(ind, 1);
    },
    //删除
    clear(state, index) {
      state.list[index].content = [];
    },
    //把所有的消息都清空了
  },
  actions: {},
  modules: {},
  plugins: [vuexPer()],
});
